import 'dart:async';
import 'package:flutter/material.dart';
import 'package:shared_preferences/shared_preferences.dart';
import 'package:flutter_swiper/flutter_swiper.dart';
import 'package:common_utils/common_utils.dart';
import 'package:splash/utils/index.dart';

class SplashPage extends StatefulWidget {
  SplashPage({Key key}) : super(key: key);
  @override
  _SplashPageState createState() => _SplashPageState();
}

class _SplashPageState extends State<SplashPage> {
  bool _isShow = false;

  List<String> _guideList = [
    Utils.getImgPath('common_loading_icon_01@3x'),
    Utils.getImgPath('common_loading_icon_02@3x'),
    Utils.getImgPath('common_loading_icon_03@3x'),
    Utils.getImgPath('common_loading_icon_04@3x'),
  ];
  List<String> _titleList = ['欢迎使用', '币币交易重磅上线', '场外交易即将开放', '随时随地了解行情'];
  List<String> _contextList = [
    '快速买卖安全可信赖的数字资产交易所，冷存储、 多重加密、银行级安全保障',
    '支持BTC/USDT 、ETH/USDT 、LTC/USDT等 多种交易对',
    '买卖自由，快速成交，安全可靠，支持全球法币         ',
    '掌上K线，市场数据，多维分析，帮助您随时随地获得利好信息'
  ];
  List<Widget> _bannerList = new List();

  @override
  void initState() {
    super.initState();
    _initSplash();
  }

  void _goMian() {
    Navigator.of(context).pushReplacementNamed('/MainPage');
   
  }

  void _initBannerData() {
    for (int i = 0, length = _guideList.length; i < length; i++) {
      if (i == (length - 1)) {
        _bannerList.add(new Container(
          child: new Stack(
            children: <Widget>[
              new Image.asset(
                _guideList[i],
                fit: BoxFit.fitWidth,
                height: double.infinity,
              ),
              new Positioned(
                  height: 130.0,
                  left: 34.0,
                  right: 34.0,
                  bottom: 170.0,
                  child: new Column(
                    children: <Widget>[
                      new Padding(
                        padding: const EdgeInsets.only(bottom: 12.5),
                        child: Text(
                          _titleList[i],
                          style: new TextStyle(
                            color: Color(0xFFFFFFFF),
                            fontSize: 20.0,
                          ),
                          textAlign: TextAlign.center,
                        ),
                      ),
                      new Text(
                        _contextList[i],
                        style: new TextStyle(
                            color: Color(0xFFFFFFFF),
                            fontSize: 14.0,
                            height: 1.1),
                        textAlign: TextAlign.center,
                      ),
                    ],
                  )),
              new Align(
                alignment: Alignment.bottomCenter,
                child: new Container(
                  margin: EdgeInsets.only(bottom: 57.0),
                  child: InkWell(
                    onTap: () {
                      _goMian();
                    },
                    child: new Container(
                      padding: EdgeInsets.fromLTRB(43.0, 12.0, 43.0, 12.0),
                      child: new Text('立即体验',
                          style: new TextStyle(
                              fontSize: 14.0, color: Color(0xFF33363A))),
                      decoration: new BoxDecoration(
                          gradient: RadialGradient(
                            colors: [Color(0xFFFFE254), Color(0xFFFFC12A)],
                          ),
                          borderRadius: BorderRadius.all(Radius.circular(4.0))),
                    ),
                  ),
                ),
              )
            ],
          ),
          color: Color(0xFF1E2125),
        ));
      } else {
        _bannerList.add(new Container(
          color: Color(0xFF1E2125),
          child: new Stack(
            children: <Widget>[
              new Image.asset(
                _guideList[i],
                fit: BoxFit.fitWidth,
                height: double.infinity,
              ),
              new Positioned(
                  height: 130.0,
                  left: 34.0,
                  right: 34.0,
                  bottom: 170.0,
                  child: new Column(
                    children: <Widget>[
                      new Padding(
                        padding: const EdgeInsets.only(bottom: 12.5),
                        child: Text(
                          _titleList[i],
                          style: new TextStyle(
                            color: Color(0xFFFFFFFF),
                            fontSize: 20.0,
                          ),
                          textAlign: TextAlign.center,
                        ),
                      ),
                      new Text(
                        _contextList[i],
                        style: new TextStyle(
                            color: Color(0xFFFFFFFF),
                            fontSize: 14.0,
                            height: 1.1),
                        textAlign: TextAlign.center,
                      ),
                    ],
                  )),
            ],
          ),
        ));
      }
    }
  }

  void _ininBanner() {
    _initBannerData();
  }

  void _initSplash() async {
    SharedPreferences prefs = await SharedPreferences.getInstance();
    Future.delayed(new Duration(milliseconds: 3000), () {
      prefs.remove('splash_bol');
      if (prefs.getBool('splash_bol') == null) {
        setState(() {
          _isShow = true;
        });
        _ininBanner();
      } else {
        _goMian();
        //Navigator.of(context).pushReplacementNamed('/MainPage ');
      }
      prefs.setBool('splash_bol', true);
      print(prefs.getBool('splash_bol'));
    });
  }

  @override
  Widget build(BuildContext context) {
    return new Material(
      child: new Stack(children: <Widget>[
        new Offstage(offstage: _isShow, child: _buildSplash()),
        new Offstage(
          offstage: !_isShow,
          child: ObjectUtil.isEmpty(_bannerList)
              ? new Container()
              : Swiper(
                  itemCount: 4,
                  itemBuilder: (BuildContext context, int index) {
                    return _bannerList[index];
                  },
                  pagination: SwiperPagination(
                      alignment: Alignment.bottomCenter,
                      margin: const EdgeInsets.fromLTRB(0, 0, 30, 10),
                      builder: DotSwiperPaginationBuilder(
                          color: Colors.black54, activeColor: Colors.white)),
                  controller: SwiperController(),
                  scrollDirection: Axis.horizontal,
                  autoplay: false,
                  onTap: (index) => print('点击了第$index'),
                ),
        ),
        _skipWidget()
      ]),
    );
  }

  Widget _buildSplash() {
    return Stack(
      alignment: Alignment.center,
      children: <Widget>[
        new Positioned(
          bottom: 50.0,
          child: new Image.asset(
            Utils.getImgPath('default_logo@3x'),
            width: 100.0,
            height: 110.0,
            fit: BoxFit.fill,
          ),
        ),
      ],
    );
  }

  Widget _skipWidget() {
    return _isShow
        ? new Positioned(
            top: 25.0,
            right: 25.0,
            child: new Container(
                alignment: Alignment.bottomRight,
                child: InkWell(
                    onTap: () {
                      _goMian();
                    },
                    child: new Container(
                        padding: EdgeInsets.fromLTRB(16.0, 8.0, 16.0, 8.0),
                        child: new Text('跳过',
                            style: new TextStyle(
                                fontSize: 14.0, color: Color(0xFF4E535A))),
                        decoration: new BoxDecoration(
                            color: Color(0xFF1E2125),
                            borderRadius:
                                BorderRadius.all(Radius.circular(30.0)),
                            border: new Border.all(
                                width: 0.33, color: Color(0xFF4E535A)))))))
        : new Container();
  }
}
